Skip to content

Media queries - #1

Draft
zoontek wants to merge 6 commits into
mainfrom
media-queries
Draft

zoontek wants to merge 6 commits into
mainfrom
media-queries

Conversation

@zoontek

@zoontek zoontek commented Nov 12, 2026

Copy link
Copy Markdown
Owner

No description provided.

zoontek pushed a commit that referenced this pull request Nov 25, 2026
…/Unimplemented fallbacks

## Summary:

`-[RCTComponentViewFactory _registerComponentIfPossible:]` runs on the JS thread, from the `ComponentDescriptorProviderRegistry` request callback. Its legacy-interop fallback and its `UnimplementedView` fallback write `_componentViewClasses` and `_registrationStatusMap` **without taking `_mutex`**. Its early `_registrationStatusMap.find` and `-_wasComponentRegistered:` also read without the lock.

On the main thread, `-createComponentViewWithComponentHandle:` takes only `std::shared_lock lock(_mutex)` before it calls `_componentViewClasses.find(handle)`. So an unlocked insert, particularly one that rehashes the map, can make that lookup miss a handle registered long ago. In practice the missed handle is often the built-in `Paragraph`:

- Debug builds abort with `RCTAssert: ComponentView with componentHandle … (Paragraph) not found.`
- Release builds compile the assert out and dereference the end iterator. react#58299 reports a production SIGSEGV at fault address `0x18`.

Any app that renders a component through the legacy interop layer is exposed: a library without Fabric codegen, `RNCMaskedView`, or any host name that resolves to `UnimplementedView`. The first render of each such name is a writer.

This PR takes the factory lock only around the map reads and writes:

- `std::shared_lock` in `-_wasComponentRegistered:` and around the early status-map check.
- In both fallbacks, the provider and class descriptor are built and `_providerRegistry.add(provider)` is called with no factory lock held. Then `std::unique_lock` is taken for the two map writes only.

The ordering is safe. `_registerComponentIfPossible:` runs synchronously inside `ComponentDescriptorRegistry::at` on the JS thread, and returns only after the writes, so the new name cannot be used before the maps hold it. Upstream already adds the provider before writing the maps.

The lock deliberately does **not** span `_providerRegistry.add`, unlike the fix proposed in react#58299. Building a `LegacyViewManagerInteropComponentDescriptor` can lazily load a view manager that requires main-queue setup (`RCTUnsafeExecuteOnMainQueueSync`; see react#53128). Holding the factory lock across that would deadlock against a main thread waiting for the lock in `-createComponentViewWithComponentHandle:`. With this change, nothing but map operations runs under the lock.

Fixes react#60244 (re-files react#58299) (closed by the bot for lack of a reproducer; the race is still present on `main`).

## Changelog:

[IOS] [FIXED] - Fix a data race in `RCTComponentViewFactory` where legacy-interop and Unimplemented component registration wrote the component registry without its lock, which could crash view creation ("ComponentView … (Paragraph) not found").

## Test Plan:

**ThreadSanitizer, before and after.** Setup:

- React Native built from source for the iOS simulator, iOS 27.2 runtime, Xcode 27.2.
- `-fsanitize=thread` added as a per-file compiler flag on `RCTComponentViewFactory.mm`, plus the link flag.
- A dev-only stress screen in a bridgeless app:
  - mounts a batch of 1,500 `<Text>` on alternate frames, more than the 1,024-view recycle pool, so the main thread keeps creating views;
  - right after each batch commits, on the JS thread, creates detached shadow nodes under host names never seen before (`nativeFabricUIManager.createNode(tag, "Probe" + n, rootTag, {}, {})`). Each one forces an `UnimplementedView` fallback registration.

**Without this change,** TSan reports:

```
WARNING: ThreadSanitizer: data race
  Read of size 8 by main thread:
    #0-#1 std::unordered_map<long long, RCTComponentViewClassDescriptor>::find
    react#2 -[RCTComponentViewFactory createComponentViewWithComponentHandle:]
    react#3 -[RCTComponentViewRegistry _dequeueComponentViewWithComponentHandle:]
  Previous write of size 8 by thread T15:
    #0-react#3 std::unordered_map<…RCTComponentViewClassDescriptor…> insert
    react#4 -[RCTComponentViewFactory _registerComponentIfPossible:]   (UnimplementedView fallback)
    … ComponentDescriptorProviderRegistry request callback
```

**With this change,** the same build, harness and run report no data race.

**Also run:** the app's full Jest suite and its iOS end-to-end flows on the patched source build.

The stress harness drives only the `UnimplementedView` fallback, because fresh legacy view-manager names can't be minted at runtime. The legacy-interop fallback gets the same lock shape, and is covered by code reading rather than by a TSan run.

Also unlocked but **not** changed here: `+[RCTLegacyViewManagerInteropComponentView isSupported:]` writes a static `NSMutableDictionary`. Its only caller is the legacy-interop fallback, on the JS thread.

Differential Revision: D134467315

Pull Request resolved: react#60243
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant